package com.website.web.front.controller;

import com.website.common.core.controller.BaseController;
import com.website.common.core.page.PageDomain;
import com.website.common.core.page.TableSupport;
import com.website.custom.domain.WConfig;
import com.website.custom.domain.WNews;
import com.website.custom.service.IWConfigService;
import com.website.custom.service.IWNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class NewsController extends BaseController {

    @Autowired
    private IWNewsService wNewsService;
    @Autowired
    private IWConfigService wConfigService;

    @GetMapping("/news")
    public String about(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        // 获取新闻列表
        startPage();
        List<WNews> wNews = wNewsService.selectWNewsList(null);
        int newsCount = wNewsService.newsCont();
        model.put("newsList", wNews);
        WConfig wConfig = wConfigService.selectWConfigById(1l);
        model.put("wConfig",wConfig);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        model.addAttribute("current", pageNum);
        model.addAttribute("caseCount", newsCount);
        return "front/news";
    }

    // 查看详情
    @GetMapping("/news/details")
    public String details(HttpServletRequest request, @RequestParam("newsId") String newsId, HttpServletResponse response, ModelMap model) {
        WNews wNews = wNewsService.selectWNewsById(Long.parseLong(newsId));
        WNews prewNews = wNewsService.selectWNewsById(wNews.getPrewId());
        WNews nextNews = wNewsService.selectWNewsById(wNews.getNextId());
        WConfig wConfig = wConfigService.selectWConfigById(1l);
        model.put("wConfig",wConfig);
        model.put("news", wNews);
        model.put("prewNews", prewNews);
        model.put("nextNews", nextNews);
        return "front/news_details";
    }
}
